/** =============Ext_.util Begin================================================================================================* */
/**
 * Use some Ext_ features
 *
 * @type
 */
Ext_ = {};

Ext_.apply = function(o, c, defaults) {
	// no "this" reference for friendly out of scope calls
	if (defaults) {
		Ext_.apply(o, defaults);
	}
	if (o && c && typeof c == 'object') {
		for (var p in c) {
			o[p] = c[p];
		}
	}
	return o;
};

(function() {
	var toString = Object.prototype.toString, ua = navigator.userAgent.toLowerCase(), check = function(r) {
		return r.test(ua);
	}, DOC = document, docMode = DOC.documentMode, isWebKit = check(/webkit/), isChrome = check(/\bchrome\b/), isGecko = !isWebKit && check(/gecko/), isOpera = check(/opera/), isIE = !isOpera && check(/msie/), isIE7 = isIE && (check(/msie 7/) || docMode == 7), isIE8 = isIE && (check(/msie 8/) && docMode != 7), isIE9 = isIE && check(/msie 9/), isIE6 = isIE && !isIE7 && !isIE8 && !isIE9;

	Ext_.apply(Ext_, {
		namespace : function() {
			var len1 = arguments.length, i = 0, len2, j, main, ns, sub, current;

			for (; i < len1; ++i) {
				main = arguments[i];
				ns = arguments[i].split('.');
				current = window[ns[0]];
				if (current === undefined) {
					current = window[ns[0]] = {};
				}
				sub = ns.slice(1);
				len2 = sub.length;
				for ( j = 0; j < len2; ++j) {
					current = current[sub[j]] = current[sub[j]] || {};
				}
			}
			return current;
		},
		urlEncode : function(o, pre) {
			var empty, buf = [], e = encodeURIComponent;

			Ext_.iterate(o, function(key, item) {
				empty = Ext_.isEmpty(item);
				Ext_.each( empty ? key : item, function(val) {
					buf.push('&', e(key), '=', (!Ext_.isEmpty(val) && (val != key || !empty)) ? (Ext_.isDate(val) ? Ext_.encode(val).replace(/"/g, '') : e(val)) : '');
				});
			});
			if (!pre) {
				buf.shift();
				pre = '';
			}
			return pre + buf.join('');
		},
		isIterable : function(v) {
			//check for array or arguments
			if (Ext_.isArray(v) || v.callee) {
				return true;
			}
			//check for node list type
			if (/NodeList|HTMLCollection/.test(toString.call(v))) {
				return true;
			}
			//NodeList has an item and length property
			//IXMLDOMNodeList has nextNode method, needs to be checked first.
			return (( typeof v.nextNode != 'undefined' || v.item) && Ext_.isNumber(v.length));
		},
		each : function(array, fn, scope) {
			if (Ext_.isEmpty(array, true)) {
				return;
			}
			if (!Ext_.isIterable(array) || Ext_.isPrimitive(array)) {
				array = [array];
			}
			for (var i = 0, len = array.length; i < len; i++) {
				if (fn.call(scope || array[i], array[i], i, array) === false) {
					return i;
				};
			}
		},
		iterate : function(obj, fn, scope) {
			if (Ext_.isEmpty(obj)) {
				return;
			}
			if (Ext_.isIterable(obj)) {
				Ext_.each(obj, fn, scope);
				return;
			} else if ( typeof obj == 'object') {
				for (var prop in obj) {
					if (obj.hasOwnProperty(prop)) {
						if (fn.call(scope || obj, prop, obj[prop], obj) === false) {
							return;
						};
					}
				}
			}
		},
		isEmpty : function(v, allowBlank) {
			return v === null || v === undefined || ((Ext_.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
		},
		isArray : function(v) {
			return toString.apply(v) === '[object Array]';
		},
		isDate : function(v) {
			return toString.apply(v) === '[object Date]';
		},
		isObject : function(v) {
			return !!v && Object.prototype.toString.call(v) === '[object Object]';
		},
		isPrimitive : function(v) {
			return Ext_.isString(v) || Ext_.isNumber(v) || Ext_.isBoolean(v);
		},
		isFunction : function(v) {
			return toString.apply(v) === '[object Function]';
		},
		isNumber : function(v) {
			return typeof v === 'number' && isFinite(v);
		},
		isString : function(v) {
			return typeof v === 'string';
		},
		isBoolean : function(v) {
			return typeof v === 'boolean';
		},
		isChrome : isChrome,
		isGecko : isGecko,
		isIE : isIE,
		isIE6 : isIE6,
		isIE7 : isIE7,
		isIE8 : isIE8,
		isIE9 : isIE9,
		isDefined : function(v) {
			return typeof v !== 'undefined';
		},
		applyIf : function(o, c) {
			if (o) {
				for (var p in c) {
					if (!Ext_.isDefined(o[p])) {
						o[p] = c[p];
					}
				}
			}
			return o;
		},
		Ajax : {
			conf : {
				method : "POST",
				url : "",
				async : true,
				scope : this,
				rest : true,
				headers : {},
				params : {}
			},

			request : function(c) {
				Ext_.applyIf(c, this.conf);
				// apply default conf

				var xhr = this._getAjaxObj();

				var onXhrReadyStateChange = function() {
					if (xhr.readyState == 4) {
						// success
						if (xhr.status == 200 || xhr.status == 201 || xhr.status == 204 || xhr.status == 202) {
							var xhr_ = this.processXhrResponse(xhr);

							if (c.rest) {
								xhr_.responseText = xhr_.responseText.replace(/[\r\n]\s*/g, "");
								// 替换回车换行
							}

							if ( typeof c.success == "function") {
								c.success.call(c.scope, xhr_);
							}
							delete xhr.onreadystatechange;
							delete xhr_;
							delete xhr;

						}
						// failure
						else if (xhr.status == 400 || xhr.status == 401 || xhr.status == 404 || xhr.status == 405 || xhr.status == 0) {
							var xhr_ = this.processXhrResponse(xhr);

							if ( typeof c.failure == "function") {
								c.failure.call(c.scope, xhr_);
							}
							delete xhr.onreadystatechange;
							delete xhr_;
							delete xhr;
						}
					}
				}
				xhr.open(c.method, c.url, c.async);
				for (var p in c.headers) {
					xhr.setRequestHeader(p, c.headers[p]);
				}
				//debugger;
				xhr.send(Ext_.util.encode(c.params));
				if (c.async) {
					xhr.onreadystatechange = (onXhrReadyStateChange).createDelegate(this);

				} else {
					(onXhrReadyStateChange).createDelegate(this)();
				}

			},
			processXhrResponse : function(xhr) {

				// responseText is READONLY，so make a copy to manipulate
				var xhr_ = {
					status : xhr.status,
					responseText : xhr.responseText,
					responseHeader : xhr.getAllResponseHeaders()
				};

				return xhr_;
			},
			_getAjaxObj : function() {
				var xmlHttp;
				if (window.XMLHttpRequest) {
					xmlHttp = new XMLHttpRequest();
				} else if (window.ActiveXObject) {
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
					if (!xmlHttp) {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				}
				return xmlHttp;
			}
		}
	});
	Ext_.ns = Ext_.namespace;
})();

Ext_.namespace("Ext_.util");

$ = function(id) {
	var hiddenEl = document.createElement('span');
	hiddenEl.hidden = true;
	return document.getElementById(id) || hiddenEl;
};
Ext_.apply(Function.prototype, {
	createInterceptor : function(fcn, scope) {
		var method = this;
		return !Ext_.isFunction(fcn) ? this : function() {
			var me = this, args = arguments;
			fcn.target = me;
			fcn.method = method;
			return (fcn.apply(scope || me || window, args) !== false) ? method.apply(me || window, args) : null;
		};
	},
	createCallback : function(/*args...*/
	) {
		// make args available, in function below
		var args = arguments, method = this;
		return function() {
			return method.apply(window, args);
		};
	},
	createDelegate : function(obj, args, appendArgs) {
		var method = this;
		return function() {
			var callArgs = args || arguments;
			if (appendArgs === true) {
				callArgs = Array.prototype.slice.call(arguments, 0);
				callArgs = callArgs.concat(args);
			} else if (Ext_.isNumber(appendArgs)) {
				callArgs = Array.prototype.slice.call(arguments, 0);
				// copy arguments first
				var applyArgs = [appendArgs, 0].concat(args);
				// create method call params
				Array.prototype.splice.apply(callArgs, applyArgs);
				// splice them in
			}
			return method.apply(obj || window, callArgs);
		};
	},

	defer : function(millis, obj, args, appendArgs) {
		var fn = this.createDelegate(obj, args, appendArgs);
		if (millis > 0) {
			return setTimeout(fn, millis);
		}
		fn();
		return 0;
	}
});

Ext_.apply(Ext_.util, {
	addEvent : function(el, event, fn, obj, overrideContext, bCapture) {
		var context = el, isCapture = bCapture ? bCapture : false, wrappedFn = null;

		if (overrideContext) {
			if (overrideContext === true) {
				context = obj;
			} else {
				context = overrideContext;
			}
		}
		wrappedFn = function(ev) {
			return fn.call(context, ev || window.event);
		};
		try {
			el.addEventListener(event, wrappedFn, isCapture);
		} catch (e) {
			el['on' + event] = wrappedFn;
		}
	},
	removeEvent : function(obj, type, fn) {
		if (obj.removeEventListener)
			obj.removeEventListener(type, fn, false);
		else if (obj.detachEvent) {
			obj.detachEvent("on" + type, obj[type + fn]);
			obj[type + fn] = null;
			obj["e" + type + fn] = null;
		}
	},

	// implement Ext_.util.stringify & destringify serialization
	encode : Ext_.util.encode ||
	function(obj) {
		var t = typeof (obj);
		if (t != "object" || obj === null) {
			// simple data type
			if (t == "string")
				obj = '"' + obj + '"';
			return String(obj);
		} else {
			// recurse array or object
			var n, v, json = [], arr = (obj && obj.constructor == Array);
			for (n in obj) {
				v = obj[n];
				t = typeof (v);
				if (t == "string")
					v = '"' + v + '"';
				else if (t == "object" && v !== null)
					v = Ext_.util.encode(v);
				json.push(( arr ? "" : '"' + n + '":') + String(v));
			}
			return ( arr ? "[" : "{") + String(json) + ( arr ? "]" : "}");
		}
	},

	// implement Ext_.util.stringify serialization
	decode : Ext_.util.decode ||
	function(json) {
		return json ? eval("(" + json + ")") : "";
	},

	// utility function to retrieve an expiration data in proper format;
	getExpDate : function(days, hours, minutes) {
		var expDate = new Date();
		if ( typeof (days) == "number" && typeof (hours) == "number" && typeof (hours) == "number") {
			expDate.setDate(expDate.getDate() + parseInt(days));
			expDate.setHours(expDate.getHours() + parseInt(hours));
			expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
			return expDate.toGMTString();
		}
	},

	// utility function called by getCookie()
	getCookieVal : function(offset) {
		var endstr = document.cookie.indexOf(";", offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	},

	// primary function to retrieve cookie by name
	getCookie : function(name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) {
				return Ext_.util.getCookieVal(j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0)
				break;
		}
		return;
	},

	// store cookie value with optional details as needed
	setCookie : function(name, value, expires, path, domain, secure) {
		document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toLocaleString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	},

	// remove the cookie by setting ancient expiration date
	deleteCookie : function(name, path, domain) {
		if (Ext_.util.getCookie(name)) {
			document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	},
	// set select's option according to given value
	SelectItemByText : function(obj, text) {
		var isExit = false;
		for (var i = 0; i < obj.options.length; i++) {
			if (obj.options[i].text == text) {
				obj.options[i].selected = true;
				return true;
			}
		}
		return false;

	}
});